{
  "cells": [
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "d2de8df3",
      "metadata": {
        "id": "d2de8df3"
      },
      "source": [
        "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/pinecone-io/examples/blob/master/learn/recommendation/audio-recommender/audio-recommendation.ipynb) [![Open nbviewer](https://raw.githubusercontent.com/pinecone-io/examples/master/assets/nbviewer-shield.svg)](https://nbviewer.org/github/pinecone-io/examples/blob/master/learn/recommendation/audio-recommender/audio-recommendation.ipynb)\n",
        "\n",
        "# Leverage the Power of Large Language Models and Vector Database to Build a Robust Audio Recommendation System"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "f0e09c9d",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "f0e09c9d",
        "outputId": "8ec93b4e-07d9-449a-e56b-2d9491d5713e"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
            "Requirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (1.5.3)\n",
            "Requirement already satisfied: python-dateutil>=2.8.1 in /usr/local/lib/python3.10/dist-packages (from pandas) (2.8.2)\n",
            "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas) (2022.7.1)\n",
            "Requirement already satisfied: numpy>=1.21.0 in /usr/local/lib/python3.10/dist-packages (from pandas) (1.22.4)\n",
            "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.8.1->pandas) (1.16.0)\n",
            "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
            "Collecting openai-whisper\n",
            "  Downloading openai-whisper-20230314.tar.gz (792 kB)\n",
            "     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 792.9/792.9 kB 14.6 MB/s eta 0:00:00\n",
            "  Installing build dependencies: started\n",
            "  Installing build dependencies: finished with status 'done'\n",
            "  Getting requirements to build wheel: started\n",
            "  Getting requirements to build wheel: finished with status 'done'\n",
            "  Preparing metadata (pyproject.toml): started\n",
            "  Preparing metadata (pyproject.toml): finished with status 'done'\n",
            "Requirement already satisfied: triton==2.0.0 in /usr/local/lib/python3.10/dist-packages (from openai-whisper) (2.0.0)\n",
            "Requirement already satisfied: numba in /usr/local/lib/python3.10/dist-packages (from openai-whisper) (0.56.4)\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from openai-whisper) (1.22.4)\n",
            "Requirement already satisfied: torch in /usr/local/lib/python3.10/dist-packages (from openai-whisper) (2.0.1+cu118)\n",
            "Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from openai-whisper) (4.65.0)\n",
            "Requirement already satisfied: more-itertools in /usr/local/lib/python3.10/dist-packages (from openai-whisper) (9.1.0)\n",
            "Collecting tiktoken==0.3.1 (from openai-whisper)\n",
            "  Downloading tiktoken-0.3.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB)\n",
            "     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.7/1.7 MB 66.3 MB/s eta 0:00:00\n",
            "Collecting ffmpeg-python==0.2.0 (from openai-whisper)\n",
            "  Downloading ffmpeg_python-0.2.0-py3-none-any.whl (25 kB)\n",
            "Requirement already satisfied: future in /usr/local/lib/python3.10/dist-packages (from ffmpeg-python==0.2.0->openai-whisper) (0.18.3)\n",
            "Requirement already satisfied: regex>=2022.1.18 in /usr/local/lib/python3.10/dist-packages (from tiktoken==0.3.1->openai-whisper) (2022.10.31)\n",
            "Requirement already satisfied: requests>=2.26.0 in /usr/local/lib/python3.10/dist-packages (from tiktoken==0.3.1->openai-whisper) (2.27.1)\n",
            "Requirement already satisfied: cmake in /usr/local/lib/python3.10/dist-packages (from triton==2.0.0->openai-whisper) (3.25.2)\n",
            "Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from triton==2.0.0->openai-whisper) (3.12.0)\n",
            "Requirement already satisfied: lit in /usr/local/lib/python3.10/dist-packages (from triton==2.0.0->openai-whisper) (16.0.5)\n",
            "Requirement already satisfied: llvmlite<0.40,>=0.39.0dev0 in /usr/local/lib/python3.10/dist-packages (from numba->openai-whisper) (0.39.1)\n",
            "Requirement already satisfied: setuptools in /usr/local/lib/python3.10/dist-packages (from numba->openai-whisper) (67.7.2)\n",
            "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.10/dist-packages (from torch->openai-whisper) (4.5.0)\n",
            "Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch->openai-whisper) (1.11.1)\n",
            "Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch->openai-whisper) (3.1)\n",
            "Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch->openai-whisper) (3.1.2)\n",
            "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.26.0->tiktoken==0.3.1->openai-whisper) (1.26.15)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.26.0->tiktoken==0.3.1->openai-whisper) (2022.12.7)\n",
            "Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.10/dist-packages (from requests>=2.26.0->tiktoken==0.3.1->openai-whisper) (2.0.12)\n",
            "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.26.0->tiktoken==0.3.1->openai-whisper) (3.4)\n",
            "Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch->openai-whisper) (2.1.2)\n",
            "Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy->torch->openai-whisper) (1.3.0)\n",
            "Building wheels for collected packages: openai-whisper\n",
            "  Building wheel for openai-whisper (pyproject.toml): started\n",
            "  Building wheel for openai-whisper (pyproject.toml): finished with status 'done'\n",
            "  Created wheel for openai-whisper: filename=openai_whisper-20230314-py3-none-any.whl size=796910 sha256=bce1943de4fe6ef685b2daf8ac6a7c7f544e2c99eecdbcea98b650417716551e\n",
            "  Stored in directory: /root/.cache/pip/wheels/b2/13/5f/fe8245f6dc59df505879da4b2129932e342f02a80e6b87f27d\n",
            "Successfully built openai-whisper\n",
            "Installing collected packages: ffmpeg-python, tiktoken, openai-whisper\n",
            "Successfully installed ffmpeg-python-0.2.0 openai-whisper-20230314 tiktoken-0.3.1\n",
            "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
            "Collecting pytube\n",
            "  Downloading pytube-15.0.0-py3-none-any.whl (57 kB)\n",
            "     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 57.6/57.6 kB 3.2 MB/s eta 0:00:00\n",
            "Installing collected packages: pytube\n",
            "Successfully installed pytube-15.0.0\n",
            "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (1.22.4)\n",
            "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
            "Collecting pinecone-client\n",
            "  Downloading pinecone_client-2.2.1-py3-none-any.whl (177 kB)\n",
            "     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 177.2/177.2 kB 5.4 MB/s eta 0:00:00\n",
            "Requirement already satisfied: requests>=2.19.0 in /usr/local/lib/python3.10/dist-packages (from pinecone-client) (2.27.1)\n",
            "Requirement already satisfied: pyyaml>=5.4 in /usr/local/lib/python3.10/dist-packages (from pinecone-client) (6.0)\n",
            "Collecting loguru>=0.5.0 (from pinecone-client)\n",
            "  Downloading loguru-0.7.0-py3-none-any.whl (59 kB)\n",
            "     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 60.0/60.0 kB 9.2 MB/s eta 0:00:00\n",
            "Requirement already satisfied: typing-extensions>=3.7.4 in /usr/local/lib/python3.10/dist-packages (from pinecone-client) (4.5.0)\n",
            "Collecting dnspython>=2.0.0 (from pinecone-client)\n",
            "  Downloading dnspython-2.3.0-py3-none-any.whl (283 kB)\n",
            "     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 283.7/283.7 kB 15.5 MB/s eta 0:00:00\n",
            "Requirement already satisfied: python-dateutil>=2.5.3 in /usr/local/lib/python3.10/dist-packages (from pinecone-client) (2.8.2)\n",
            "Requirement already satisfied: urllib3>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from pinecone-client) (1.26.15)\n",
            "Requirement already satisfied: tqdm>=4.64.1 in /usr/local/lib/python3.10/dist-packages (from pinecone-client) (4.65.0)\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from pinecone-client) (1.22.4)\n",
            "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.5.3->pinecone-client) (1.16.0)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.19.0->pinecone-client) (2022.12.7)\n",
            "Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.10/dist-packages (from requests>=2.19.0->pinecone-client) (2.0.12)\n",
            "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.19.0->pinecone-client) (3.4)\n",
            "Installing collected packages: loguru, dnspython, pinecone-client\n",
            "Successfully installed dnspython-2.3.0 loguru-0.7.0 pinecone-client-2.2.1\n"
          ]
        }
      ],
      "source": [
        "%%bash\n",
        "pip install pandas\n",
        "pip install -U openai-whisper\n",
        "pip install pytube\n",
        "pip install numpy\n",
        "pip install pinecone-client"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "id": "16fb4387",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "16fb4387",
        "outputId": "6d5b053c-5ef9-4483-e3d9-f2223a28e6b7"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "/usr/local/lib/python3.10/dist-packages/pinecone/index.py:4: TqdmExperimentalWarning: Using `tqdm.autonotebook.tqdm` in notebook mode. Use `tqdm.tqdm` instead to force console mode (e.g. in jupyter console)\n",
            "  from tqdm.autonotebook import tqdm\n"
          ]
        }
      ],
      "source": [
        "# Import the modules\n",
        "import os\n",
        "import torch\n",
        "import whisper\n",
        "from pinecone import Pinecone\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "from pytube import YouTube"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "344f8d35",
      "metadata": {
        "id": "344f8d35"
      },
      "outputs": [],
      "source": [
        "def video_to_audio(video_url, destination):\n",
        "\n",
        "    # Get the video\n",
        "    video = YouTube(video_url)\n",
        "\n",
        "    # Convert video to Audio\n",
        "    audio = video.streams.filter(only_audio=True).first()\n",
        "\n",
        "    # Save to destination\n",
        "    output = audio.download(output_path = destination)\n",
        "\n",
        "    name, ext = os.path.splitext(output)\n",
        "    new_file = name + '.mp3'\n",
        "\n",
        "    # Replace spaces with \"_\"\n",
        "    new_file = new_file.replace(\" \", \"_\")\n",
        "\n",
        "    # Change the name of the file\n",
        "    os.rename(output, new_file)\n",
        "\n",
        "    return new_file"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "id": "878139fb",
      "metadata": {
        "id": "878139fb"
      },
      "outputs": [],
      "source": [
        "%%bash\n",
        "mkdir \"audio_data\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "id": "5ebb07b5",
      "metadata": {
        "id": "5ebb07b5"
      },
      "outputs": [],
      "source": [
        "# Create URL column\n",
        "audio_path = \"audio_data\"\n",
        "\n",
        "# Have just provided a sample of links for experimentation purpose\n",
        "list_videos = [\"https://www.youtube.com/watch?v=IdTMDpizis8\",\n",
        "              \"https://www.youtube.com/watch?v=fLeJJPxua3E\",\n",
        "              \"https://www.youtube.com/watch?v=z3FA2kALScU\",\n",
        "              \"https://www.youtube.com/watch?v=yBrRpb8aLwk\",\n",
        "              \"https://www.youtube.com/watch?v=ERClHCOF14c\",\n",
        "              \"https://www.youtube.com/watch?v=b-Pn0yXL9y8\",\n",
        "              \"https://www.youtube.com/watch?v=CYfU9WBy_HA\",\n",
        "              \"https://www.youtube.com/watch?v=FncTDZxNbM4\",\n",
        "              \"https://www.youtube.com/watch?v=JjCFoba5hKE\",\n",
        "              \"https://www.youtube.com/watch?v=YUxR3d10lz4\",\n",
        "              \"https://www.youtube.com/watch?v=t1XCzWlYWeA\",\n",
        "              \"https://www.youtube.com/watch?v=scvDvVbv1hk\",\n",
        "              \"https://www.youtube.com/watch?v=z3FA2kALScU&t\", \n",
        "              \"https://www.youtube.com/watch?v=wsNzAuYDgy0\", \n",
        "              \"https://www.youtube.com/watch?v=R0Sq_x6eObE\", \n",
        "              \"https://www.youtube.com/watch?v=RLTgnOuYb6o&t\", \n",
        "              \"https://www.youtube.com/watch?v=UF8uR6Z6KLc\", \n",
        "              \"https://www.youtube.com/watch?v=aF1_oLdruSc\", \n",
        "              \"https://www.youtube.com/watch?v=1bumPyvzCyo\", \n",
        "              \"https://www.youtube.com/watch?v=YUxR3d10lz4&t\", \n",
        "              \"https://www.youtube.com/watch?v=gMWXMMUg5pI\"]\n",
        "# Create dataframe\n",
        "transcription_df = pd.DataFrame(list_videos, columns=['URLs'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "id": "9d960a1c",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 206
        },
        "id": "9d960a1c",
        "outputId": "478188e6-dc26-4618-878f-b4299f5d6bde"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "  <div id=\"df-c0bbdccc-d39c-4dba-872c-6c3c419cfb2d\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>URLs</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>https://www.youtube.com/watch?v=IdTMDpizis8</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>https://www.youtube.com/watch?v=fLeJJPxua3E</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>https://www.youtube.com/watch?v=z3FA2kALScU</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>https://www.youtube.com/watch?v=yBrRpb8aLwk</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>https://www.youtube.com/watch?v=ERClHCOF14c</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-c0bbdccc-d39c-4dba-872c-6c3c419cfb2d')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-c0bbdccc-d39c-4dba-872c-6c3c419cfb2d button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-c0bbdccc-d39c-4dba-872c-6c3c419cfb2d');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ],
            "text/plain": [
              "                                          URLs\n",
              "0  https://www.youtube.com/watch?v=IdTMDpizis8\n",
              "1  https://www.youtube.com/watch?v=fLeJJPxua3E\n",
              "2  https://www.youtube.com/watch?v=z3FA2kALScU\n",
              "3  https://www.youtube.com/watch?v=yBrRpb8aLwk\n",
              "4  https://www.youtube.com/watch?v=ERClHCOF14c"
            ]
          },
          "execution_count": 10,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "transcription_df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "id": "09335127",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 206
        },
        "id": "09335127",
        "outputId": "8ebda7a2-8745-4215-b45d-f5a6a22dcb33"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "  <div id=\"df-9bfee911-8b73-41a6-8c69-f07faff32081\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>URLs</th>\n",
              "      <th>file_name</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>https://www.youtube.com/watch?v=IdTMDpizis8</td>\n",
              "      <td>/content/audio_data/Jocko_Willink_GOOD_(Offici...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>https://www.youtube.com/watch?v=fLeJJPxua3E</td>\n",
              "      <td>/content/audio_data/Best_Short_Motivational_Sp...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>https://www.youtube.com/watch?v=z3FA2kALScU</td>\n",
              "      <td>/content/audio_data/STOP_WASTING_TIME_-_Part_1...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>https://www.youtube.com/watch?v=yBrRpb8aLwk</td>\n",
              "      <td>/content/audio_data/YOUR_VALUE_-_Powerful_Moti...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>https://www.youtube.com/watch?v=ERClHCOF14c</td>\n",
              "      <td>/content/audio_data/LION_MENTALITY_-_Motivatio...</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-9bfee911-8b73-41a6-8c69-f07faff32081')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-9bfee911-8b73-41a6-8c69-f07faff32081 button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-9bfee911-8b73-41a6-8c69-f07faff32081');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ],
            "text/plain": [
              "                                          URLs  \\\n",
              "0  https://www.youtube.com/watch?v=IdTMDpizis8   \n",
              "1  https://www.youtube.com/watch?v=fLeJJPxua3E   \n",
              "2  https://www.youtube.com/watch?v=z3FA2kALScU   \n",
              "3  https://www.youtube.com/watch?v=yBrRpb8aLwk   \n",
              "4  https://www.youtube.com/watch?v=ERClHCOF14c   \n",
              "\n",
              "                                           file_name  \n",
              "0  /content/audio_data/Jocko_Willink_GOOD_(Offici...  \n",
              "1  /content/audio_data/Best_Short_Motivational_Sp...  \n",
              "2  /content/audio_data/STOP_WASTING_TIME_-_Part_1...  \n",
              "3  /content/audio_data/YOUR_VALUE_-_Powerful_Moti...  \n",
              "4  /content/audio_data/LION_MENTALITY_-_Motivatio...  "
            ]
          },
          "execution_count": 11,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Create the files_name\n",
        "transcription_df[\"file_name\"] = transcription_df[\"URLs\"].apply(lambda url: video_to_audio(url, audio_path))\n",
        "transcription_df.head()"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "25d6b37e",
      "metadata": {
        "id": "25d6b37e"
      },
      "source": [
        "## Transcription"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "id": "fad4eef4",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "fad4eef4",
        "outputId": "a4b1619d-5bd9-49f2-d2e4-7abbab06dde6"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "100%|██████████████████████████████████████| 2.87G/2.87G [00:22<00:00, 140MiB/s]\n"
          ]
        }
      ],
      "source": [
        "# Set the device\n",
        "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
        "\n",
        "# Load the model\n",
        "whisper_model = whisper.load_model(\"large\", device=device)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "id": "b8ce8178",
      "metadata": {
        "id": "b8ce8178"
      },
      "outputs": [],
      "source": [
        "def audio_to_text(audio_file):\n",
        "\n",
        "    return whisper_model.transcribe(audio_file)[\"text\"]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "id": "b016dc08",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 206
        },
        "id": "b016dc08",
        "outputId": "e8bbc785-d1cc-4861-fbc3-8d79874f5a5f"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "  <div id=\"df-88cbfedb-e6ff-4b63-9fa6-890c33b93cc8\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>URLs</th>\n",
              "      <th>file_name</th>\n",
              "      <th>transcriptions</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>https://www.youtube.com/watch?v=IdTMDpizis8</td>\n",
              "      <td>/content/audio_data/Jocko_Willink_GOOD_(Offici...</td>\n",
              "      <td>one of my direct subordinates, one of my guys...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>https://www.youtube.com/watch?v=fLeJJPxua3E</td>\n",
              "      <td>/content/audio_data/Best_Short_Motivational_Sp...</td>\n",
              "      <td>If you only have 24 hours in a day, your succ...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>https://www.youtube.com/watch?v=z3FA2kALScU</td>\n",
              "      <td>/content/audio_data/STOP_WASTING_TIME_-_Part_1...</td>\n",
              "      <td>The one commodity that is most valuable on th...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>https://www.youtube.com/watch?v=yBrRpb8aLwk</td>\n",
              "      <td>/content/audio_data/YOUR_VALUE_-_Powerful_Moti...</td>\n",
              "      <td>Let me speak to that person that feels like t...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>https://www.youtube.com/watch?v=ERClHCOF14c</td>\n",
              "      <td>/content/audio_data/LION_MENTALITY_-_Motivatio...</td>\n",
              "      <td>If the lion is the king of the jungle, how ca...</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-88cbfedb-e6ff-4b63-9fa6-890c33b93cc8')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-88cbfedb-e6ff-4b63-9fa6-890c33b93cc8 button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-88cbfedb-e6ff-4b63-9fa6-890c33b93cc8');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ],
            "text/plain": [
              "                                          URLs  \\\n",
              "0  https://www.youtube.com/watch?v=IdTMDpizis8   \n",
              "1  https://www.youtube.com/watch?v=fLeJJPxua3E   \n",
              "2  https://www.youtube.com/watch?v=z3FA2kALScU   \n",
              "3  https://www.youtube.com/watch?v=yBrRpb8aLwk   \n",
              "4  https://www.youtube.com/watch?v=ERClHCOF14c   \n",
              "\n",
              "                                           file_name  \\\n",
              "0  /content/audio_data/Jocko_Willink_GOOD_(Offici...   \n",
              "1  /content/audio_data/Best_Short_Motivational_Sp...   \n",
              "2  /content/audio_data/STOP_WASTING_TIME_-_Part_1...   \n",
              "3  /content/audio_data/YOUR_VALUE_-_Powerful_Moti...   \n",
              "4  /content/audio_data/LION_MENTALITY_-_Motivatio...   \n",
              "\n",
              "                                      transcriptions  \n",
              "0   one of my direct subordinates, one of my guys...  \n",
              "1   If you only have 24 hours in a day, your succ...  \n",
              "2   The one commodity that is most valuable on th...  \n",
              "3   Let me speak to that person that feels like t...  \n",
              "4   If the lion is the king of the jungle, how ca...  "
            ]
          },
          "execution_count": 14,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Apply the function to all the audio files\n",
        "transcription_df[\"transcriptions\"] = transcription_df[\"file_name\"].apply(lambda f_name: audio_to_text(f_name))\n",
        "\n",
        "\n",
        "# Show the first five rows\n",
        "transcription_df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 17,
      "id": "ryc6CfPxz_O4",
      "metadata": {
        "id": "ryc6CfPxz_O4"
      },
      "outputs": [],
      "source": [
        "import textwrap"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 18,
      "id": "8171d2ff",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "8171d2ff",
        "outputId": "7e474b89-4efe-4504-aad4-0ba8f8e21b32"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            " one of my direct subordinates, one of my guys that worked\n",
            "for me, he would call me up or pull me aside with some major\n",
            "problem, some issue that was going on, and he'd say, boss,\n",
            "we got this and that and the other thing and I'd look at him\n",
            "and I'd say, good. And finally one day he was telling me\n",
            "about some issue that he was having, some problem, and he\n",
            "said, I already know what you're gonna say. And I said,\n",
            "well, what am I gonna say? He said, you're gonna say good.\n",
            "He said, that's what you always say. When something is wrong\n",
            "and going bad, you always just look at me and say good. And\n",
            "I said, well, yeah. When things are going bad, there's gonna\n",
            "be some good that's gonna come from it. Didn't get the new\n",
            "high-speed gear we wanted? Good. Didn't get promoted? Good.\n",
            "More time to get better. Oh, mission got canceled? Good, we\n",
            "can focus on another one. Didn't get funded? Didn't get the\n",
            "job you wanted? Got injured? Sprained my ankle? Got tapped\n",
            "out? Good. Got beat? Good. You learned. Unexpected problems?\n",
            "Good. We have the opportunity to figure out a solution.\n",
            "That's it. When things are going bad, don't get all bummed\n",
            "out, don't get startled, don't get frustrated. If you can\n",
            "say the word good, guess what? It means you're still alive.\n",
            "It means you're still breathing. And if you're still\n",
            "breathing, well, now, you still got some fight left in you.\n",
            "So get up, dust off, reload, recalibrate, re-engage, and go\n",
            "out on the attack.\n"
          ]
        }
      ],
      "source": [
        "wrapper = textwrap.TextWrapper(width=60)\n",
        "first_transcription = transcription_df.iloc[0][\"transcriptions\"]\n",
        "formatted_transcription = wrapper.fill(text=first_transcription)\n",
        "\n",
        "# Check first transcription\n",
        "print(formatted_transcription)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 23,
      "id": "5e152c8f",
      "metadata": {
        "id": "5e152c8f"
      },
      "outputs": [],
      "source": [
        "transcription_df = transcription_df.drop([\"transcription_length\"], axis=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 28,
      "id": "PRxdwgvG2mn6",
      "metadata": {
        "id": "PRxdwgvG2mn6"
      },
      "outputs": [],
      "source": [
        "transcription_df.head()"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "eQfwmvG0-pZb",
      "metadata": {
        "id": "eQfwmvG0-pZb"
      },
      "source": [
        "## Generation of Transcripts' Embeddings "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 34,
      "id": "Jyho4nPW_KT0",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Jyho4nPW_KT0",
        "outputId": "c0b30d22-2d92-429f-ede4-2fcf06bf3aa9"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
            "Collecting openai\n",
            "  Downloading openai-0.27.7-py3-none-any.whl (71 kB)\n",
            "\u001b[?25l     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/72.0 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m72.0/72.0 kB\u001b[0m \u001b[31m3.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hRequirement already satisfied: requests>=2.20 in /usr/local/lib/python3.10/dist-packages (from openai) (2.27.1)\n",
            "Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from openai) (4.65.0)\n",
            "Collecting aiohttp (from openai)\n",
            "  Downloading aiohttp-3.8.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.0 MB)\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.0/1.0 MB\u001b[0m \u001b[31m22.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hRequirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai) (1.26.15)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai) (2022.12.7)\n",
            "Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai) (2.0.12)\n",
            "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai) (3.4)\n",
            "Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai) (23.1.0)\n",
            "Collecting multidict<7.0,>=4.5 (from aiohttp->openai)\n",
            "  Downloading multidict-6.0.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (114 kB)\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m114.5/114.5 kB\u001b[0m \u001b[31m14.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hCollecting async-timeout<5.0,>=4.0.0a3 (from aiohttp->openai)\n",
            "  Downloading async_timeout-4.0.2-py3-none-any.whl (5.8 kB)\n",
            "Collecting yarl<2.0,>=1.0 (from aiohttp->openai)\n",
            "  Downloading yarl-1.9.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (268 kB)\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m268.8/268.8 kB\u001b[0m \u001b[31m25.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hCollecting frozenlist>=1.1.1 (from aiohttp->openai)\n",
            "  Downloading frozenlist-1.3.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (149 kB)\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m149.6/149.6 kB\u001b[0m \u001b[31m16.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hCollecting aiosignal>=1.1.2 (from aiohttp->openai)\n",
            "  Downloading aiosignal-1.3.1-py3-none-any.whl (7.6 kB)\n",
            "Installing collected packages: multidict, frozenlist, async-timeout, yarl, aiosignal, aiohttp, openai\n",
            "Successfully installed aiohttp-3.8.4 aiosignal-1.3.1 async-timeout-4.0.2 frozenlist-1.3.3 multidict-6.0.4 openai-0.27.7 yarl-1.9.2\n"
          ]
        }
      ],
      "source": [
        "!pip install openai\n",
        "import openai"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 35,
      "id": "2Qwclp5v-iiI",
      "metadata": {
        "id": "2Qwclp5v-iiI"
      },
      "outputs": [],
      "source": [
        "# Set up the OpenAI key\n",
        "openai.api_key = \"<YOUR OPENAI KEY>\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 41,
      "id": "GBKs1Eiy_lJA",
      "metadata": {
        "id": "GBKs1Eiy_lJA"
      },
      "outputs": [],
      "source": [
        "def get_embeddings(text_to_embed):\n",
        "\n",
        "\tresponse = openai.Embedding.create(\n",
        "    \tmodel= \"text-embedding-ada-002\",\n",
        "    \tinput=[text_to_embed]\n",
        "\t)\n",
        "\n",
        "\t# Extract the AI output embedding as a list of floats\n",
        "\tembedding = response[\"data\"][0][\"embedding\"]\n",
        "\n",
        "\treturn embedding"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 43,
      "id": "DzpA5EYF_oec",
      "metadata": {
        "id": "DzpA5EYF_oec"
      },
      "outputs": [],
      "source": [
        "transcription_df[\"embedding\"] = transcription_df[\"transcriptions\"].astype(str).apply(get_embeddings)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 44,
      "id": "sJfPT3LT_yCm",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 337
        },
        "id": "sJfPT3LT_yCm",
        "outputId": "5850da44-cb35-44a2-a386-4acb3e418204"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "  <div id=\"df-7ea9d471-8f23-40f6-913c-bd778fa271d8\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>URLs</th>\n",
              "      <th>file_name</th>\n",
              "      <th>transcriptions</th>\n",
              "      <th>embedding</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>https://www.youtube.com/watch?v=RLTgnOuYb6o&amp;t</td>\n",
              "      <td>/content/audio_data/Oprah_Winfreys_Life_Advice...</td>\n",
              "      <td>The three things that I want to leave with yo...</td>\n",
              "      <td>[0.004491745959967375, -0.010612116195261478, ...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>https://www.youtube.com/watch?v=1bumPyvzCyo</td>\n",
              "      <td>/content/audio_data/Arnold_Schwarzenegger_Leav...</td>\n",
              "      <td>I'm here to talk about success. The first rul...</td>\n",
              "      <td>[-0.015403729863464832, -0.03353560343384743, ...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>https://www.youtube.com/watch?v=b-Pn0yXL9y8</td>\n",
              "      <td>/content/audio_data/5_Minutes_to_Start_Your_Da...</td>\n",
              "      <td>If you want to change the world, start off by...</td>\n",
              "      <td>[0.008127240464091301, 0.004852672573179007, 0...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>https://www.youtube.com/watch?v=scvDvVbv1hk</td>\n",
              "      <td>/content/audio_data/CHANGE_YOUR_MINDSET_-_Moti...</td>\n",
              "      <td>Are you a gazelle? Or are you a lion? Let me ...</td>\n",
              "      <td>[-0.01881832256913185, 0.00985600147396326, 0....</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>https://www.youtube.com/watch?v=YUxR3d10lz4</td>\n",
              "      <td>/content/audio_data/ITS_TIME_TO_GET_AFTER_IT!_...</td>\n",
              "      <td>Listen to me once you realize who you are you...</td>\n",
              "      <td>[-0.012264629825949669, -0.02990311197936535, ...</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-7ea9d471-8f23-40f6-913c-bd778fa271d8')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-7ea9d471-8f23-40f6-913c-bd778fa271d8 button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-7ea9d471-8f23-40f6-913c-bd778fa271d8');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ],
            "text/plain": [
              "                                            URLs  \\\n",
              "0  https://www.youtube.com/watch?v=RLTgnOuYb6o&t   \n",
              "1    https://www.youtube.com/watch?v=1bumPyvzCyo   \n",
              "2    https://www.youtube.com/watch?v=b-Pn0yXL9y8   \n",
              "3    https://www.youtube.com/watch?v=scvDvVbv1hk   \n",
              "4    https://www.youtube.com/watch?v=YUxR3d10lz4   \n",
              "\n",
              "                                           file_name  \\\n",
              "0  /content/audio_data/Oprah_Winfreys_Life_Advice...   \n",
              "1  /content/audio_data/Arnold_Schwarzenegger_Leav...   \n",
              "2  /content/audio_data/5_Minutes_to_Start_Your_Da...   \n",
              "3  /content/audio_data/CHANGE_YOUR_MINDSET_-_Moti...   \n",
              "4  /content/audio_data/ITS_TIME_TO_GET_AFTER_IT!_...   \n",
              "\n",
              "                                      transcriptions  \\\n",
              "0   The three things that I want to leave with yo...   \n",
              "1   I'm here to talk about success. The first rul...   \n",
              "2   If you want to change the world, start off by...   \n",
              "3   Are you a gazelle? Or are you a lion? Let me ...   \n",
              "4   Listen to me once you realize who you are you...   \n",
              "\n",
              "                                           embedding  \n",
              "0  [0.004491745959967375, -0.010612116195261478, ...  \n",
              "1  [-0.015403729863464832, -0.03353560343384743, ...  \n",
              "2  [0.008127240464091301, 0.004852672573179007, 0...  \n",
              "3  [-0.01881832256913185, 0.00985600147396326, 0....  \n",
              "4  [-0.012264629825949669, -0.02990311197936535, ...  "
            ]
          },
          "execution_count": 44,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "transcription_df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 50,
      "id": "ZykZAv0jDvbT",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ZykZAv0jDvbT",
        "outputId": "0acae12d-4d7e-4b40-ec67-5c2900b4ec99"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "1536"
            ]
          },
          "execution_count": 50,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "vector_dim = transcription_df.iloc[0].embedding\n",
        "len(vector_dim)"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "BrTLL4K-DVwx",
      "metadata": {
        "id": "BrTLL4K-DVwx"
      },
      "source": [
        "## Configure your environment"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 51,
      "id": "rhFpGuotAkbC",
      "metadata": {
        "id": "rhFpGuotAkbC"
      },
      "outputs": [],
      "source": [
        "# find API key in console at app.pinecone.io\n",
        "api_key = os.getenv('PINECONE_API_KEY') or 'PINECONE_API_KEY'\n",
        "# find ENV (cloud region) next to API key in console\n",
        "env = os.getenv('PINECONE_ENVIRONMENT') or 'PINECONE_ENVIRONMENT'\n",
        "\n",
        "# Initialize connection to pinecone\n",
        "pinecone.init(\n",
        "  api_key=api_key,\n",
        "  environment=env\n",
        ")\n",
        "\n",
        "# Index params\n",
        "my_index_name = \"audio-search\"\n",
        "vector_dim = len(transcription_df.iloc[0].embedding)\n",
        "\n",
        "if my_index_name not in pinecone.list_indexes().names():\n",
        "  # Create the index\n",
        "  pinecone.create_index(name = my_index_name,\n",
        "                      dimension=vector_dim,\n",
        "                      metric=\"cosine\", shards=1,\n",
        "                      pod_type='s1.x1')\n",
        "# Connect to the index\n",
        "my_index = pinecone.Index(index_name = my_index_name)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 52,
      "id": "h2_dlcRbDlfY",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "h2_dlcRbDlfY",
        "outputId": "f21d81ca-ff2c-48c2-ba31-e7f707e3034e"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'dimension': 1536,\n",
              " 'index_fullness': 0.0,\n",
              " 'namespaces': {},\n",
              " 'total_vector_count': 0}"
            ]
          },
          "execution_count": 52,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Show information about the vector index\n",
        "my_index.describe_index_stats()"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "id": "lY78Z58LEoJb",
      "metadata": {
        "id": "lY78Z58LEoJb"
      },
      "source": [
        "## Populate the the Pinecone Vector Index"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 54,
      "id": "t3BTJ7uWEWtv",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "t3BTJ7uWEWtv",
        "outputId": "2781476f-3024-42ed-b5f7-69bb3f30b9ca"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'dimension': 1536,\n",
              " 'index_fullness': 0.0,\n",
              " 'namespaces': {'': {'vector_count': 147}},\n",
              " 'total_vector_count': 147}"
            ]
          },
          "execution_count": 54,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "transcription_df[\"vector_id\"] = transcription_df.index\n",
        "transcription_df[\"vector_id\"] = transcription_df[\"vector_id\"].apply(str)\n",
        "\n",
        "# Get all the metadata\n",
        "final_metadata = []\n",
        "\n",
        "for index in range(len(transcription_df)):\n",
        "  final_metadata.append({\n",
        "      'ID':  index,\n",
        "      'url': transcription_df.iloc[index].URLs,\n",
        "      'transcription': transcription_df.iloc[index].transcriptions\n",
        "  })\n",
        "\n",
        "audio_IDs = transcription_df.vector_id.tolist()\n",
        "audio_embeddings = [arr for arr in transcription_df.embedding]\n",
        "\n",
        "# Create the single list of dictionary format to insert\n",
        "data_to_upsert = list(zip(audio_IDs, audio_embeddings, final_metadata))\n",
        "\n",
        "# Upload the final data\n",
        "my_index.upsert(vectors = data_to_upsert)\n",
        "\n",
        "# Show information about the vector index\n",
        "my_index.describe_index_stats()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "C-ezoAeMFEmP",
      "metadata": {
        "id": "C-ezoAeMFEmP"
      },
      "outputs": [],
      "source": [
        "N = 3\n",
        "my_query_embedding = transcription_df.embedding[0]\n",
        "\n",
        "# Run the Query Search\n",
        "my_index.query(vector=my_query_embedding, top_k=N, include_metadata=True)"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "gpuType": "T4",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.9.15"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": false,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": false
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
